package com.nowcoder.community.service;

import com.nowcoder.community.dao.TestDao;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;

/**
 * @ClassName ServiceTest
 * @Description
 * @Author hhwu
 * @Date 2022/5/12 22:29
 * Version 1.0
 **/
@Service("serviceTest")
public class TestService {

    private static final Logger logger = LoggerFactory.getLogger(TestService.class);

    public TestService() {
        System.out.println("实例化，调用该类的一个构造器时，调用该方法");
    }

    @PostConstruct// 该方法在构造器之后调用
    public void init() {
        System.out.println("@PostConstruct初始化，调用该类的构造器后，调用该方法");
    }

    @PreDestroy// 该方法在对象销毁前执行
    public void destroy() {
        System.out.println("@PreDestroy销毁，销毁该类的对象前，调用该方法");
    }

    @Autowired
    @Qualifier("testDaoImpl")
    private TestDao testDaoImpl;
    public String find() {
        return testDaoImpl.select();
    }

    @Async// 可以让该方法在spring多线程环境下被异步调用
    public void execute1() {
        logger.debug("execute1");
    }

//    @Scheduled(initialDelay = 10000, fixedRate = 1000)// 可以让该方法在spring多线程环境下被定时调用，不需要外部调用，程序启动时，该方法会被自动分配线程进行执行
    public void  execute2() {
        logger.debug("execute2");
    }
}
